﻿<cfscript>
/** 
* Chinese Info Frameworks
* 
* @hint "The Chinese Pinyin translator." 
*/ 

component 
	displayname="public.service.handler.chineseFactory" 
	output=false 
	{
	
	/**
	* @hint "初始化对象"
	*/
	public public.service.handler.chineseFactory function init() output=false {
		return this;
	}
	
	/* DEPENDS: ORM Chinese */
	
	/**
	* @hint "返回汉字拼音串"
	* 
	* @ChineseStr "中文字符串"
	*/
	public string function getPinyin( required string ChineseStr ) output=false { 
	
		var ChineseName = trim(arguments.ChineseStr);
		var py = "";
		var char = "";
		var checkQuery = [];
		var i = 1;
		
		/* 生成拼音 */
		for ( i=1; i lte len(ChineseName); i++) {
			
			char = mid(ChineseName, i, 1);
			
			if ( refind("^[A-Za-z0-9]+$", char) ) {
				py = listAppend(py, char, " ");
			}
			else {
		
				/* 索检拼音 */
				var sql = "SELECT phonetic FROM t_chinese WHERE character = :char ";
				
				var queryObj = new Query( datasource=application.dnsSlave );
				queryObj.addParam( name="char", value=char, cfsqltype="cf_sql_char" );
			
				var rs = queryObj.execute( sql=sql ).getResult();
				
				if ( rs.recordCount ) {
					if ( i lt 3 ) {
						py = listAppend(py, rs.phonetic, " ");
					}
					else {
						py = py & rs.phonetic;
					}
				}
				
			}

		}
		
		return py;
	}
	
	/**
	* @hint "返回汉字拼音首字母"
	* 
	* @ChineseStr "中文字符串"
	*/
	public string function getPinyinIndex( required string ChineseStr ) output=false { 
	
		var ChineseName = trim(arguments.ChineseStr);
		var py = "";
		var char = "";
		var checkQuery = [];
		var i = 1;
		
		/* 生成拼音 */
		for ( i=1; i lte len(ChineseName); i++) {
			
			char = mid(ChineseName, i, 1);
			
			if ( refind("^[A-Za-z0-9]+$", char) ) {
				py = py & char;
			}
			else {
		
				/* 索检拼音 */
				var sql = "SELECT charidx FROM t_chinese WHERE character = :char ";
				
				var queryObj = new Query( datasource=application.dnsSlave );
				queryObj.addParam( name="char", value=char, cfsqltype="cf_sql_char" );
			
				var rs = queryObj.execute( sql=sql ).getResult();
				
				if ( rs.recordCount ) {
					py = py & rs.charidx;
				}
				
			}

		}
		
		return py;
	}
}
</cfscript>